<?php

namespace lvzmen\yii\cms\widgets\modal;

use Yii;
use yii\bootstrap\Html;
use yii\bootstrap\Modal;

/**
 * A menu imitate the sidebar of https://docs.docker.com/get-started/
 *
 * @note It's depend on bootstrap;
 *
 * @example :
 * ```php
 * echo MyModal::widget([
 *     'header' => 'Hello world',
 *     'headerOptions' => [
 *         'style' => [
 *             'font-weight' => 600,
 *             'font-size' => '20px'
 *         ]
 *     ],
 *     'toggleButton' => [
 *         'label' => '非堆栈对话框',
 *         'class' => 'btn btn-success'
 *     ],
 *     'body' => '我就不用堆栈，你来打我啊',
 *     'footer' => Button::widget([
 *             "label" => "Confirm",
 *             "options" => [
 *                 "class" => "btn btn-primary"
 *             ]
 *         ]) .
 *         Button::widget([
 *             "label" => "Cancel",
 *             "options" => [
 *                 'data-dismiss' => "modal",
 *                 "class" => "btn btn-default"
 *             ]
 *         ])
 * ])
 * ```
 * @author chenzhiwei <chenzhiweiwk@qq.com>
 * @date   2021/08/31
 */
class MyModal extends Modal
{
    public $body = "";

    /**
     * Initializes the widget.
     */
    public function init()
    {
        if(is_null($this->getId(false))) {
            $this->setId("modal_" . uniqid());
        }
        $this->options["id"] = $this->options["id"] ?? $this->getId();
        $this->initOptions();
    }

    /**
     * Renders the widget.
     */
    public function run()
    {
        $this->registerPlugin('modal');

        $result = "";
        $result .= $this->renderToggleButton() . "\n";
        $result .= Html::beginTag('div', $this->options) . "\n";
        $result .= Html::beginTag('div', ['class' => 'modal-dialog ' . $this->size]) . "\n";
        $result .= Html::beginTag('div', ['class' => 'modal-content']) . "\n";
        $result .= $this->renderHeader() . "\n";
        $result .= $this->renderBodyBegin() . "\n";
        $result .= $this->body . "\n";
        $result .= "\n" . $this->renderBodyEnd();
        $result .= "\n" . $this->renderFooter();
        $result .= "\n" . Html::endTag('div'); // modal-content
        $result .= "\n" . Html::endTag('div'); // modal-dialog
        $result .= "\n" . Html::endTag('div');

        return $result;
    }
}
